import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/cartoon_business/acg_widget_item.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/video_type_menu_view.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';

import 'cartoon_tag_logic.dart';

class CartoonTagPage extends StatelessWidget {
  final String title;
  final String? sId;

  const CartoonTagPage({
    super.key,
    this.title = '',
    this.sId,
  });

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CartoonTagLogic>(
      init: CartoonTagLogic(sId),
      tag: "${DateTime.now().millisecondsSinceEpoch}",
      builder: (controller) {
        return Scaffold(
          appBar: AppBar(
            title: Text(title),
          ),
          body: Column(
            children: [
              Container(
                padding: EdgeInsets.only(left: 16),
                child: VideoTypeMenuView(
                  controller.sorts,
                  selectIndex: controller.sortIndex,
                  callback: (value) {
                    controller.sortIndex = value;
                    controller.fetchPageData(showLoading: true);
                  },
                ),
              ),
              Expanded(
                child: pullYsRefresh(
                  onRefresh: (_) => controller.fetchPageData(),
                  onLoading: (_) => controller.fetchPageData(isRefresh: false),
                  onInit: (ctr) => controller.refreshController = ctr,
                  child: () {
                    if (controller.isLoading) return LoadingCenterWidget();
                    if (controller.dataSource.isEmpty) return CErrorWidget();
                    return GridView.builder(
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisSpacing: 6,
                        mainAxisSpacing: 12,
                        crossAxisCount: 3,
                        childAspectRatio: 111 / 190,
                      ),
                      itemCount: controller.dataSource.length,
                      itemBuilder: (BuildContext context, int index) {
                        return AcgItemWidget(info: controller.dataSource[index]);
                      },
                    );
                  }(),
                ),
              )
            ],
          ),
        );
      },
    );
  }
}
